/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EditarCotizacion.java
 *
 * Created on 24/04/2011, 15:49:18
 */

package vista.cotizacion;

import controlador.cotizacion.GestorEditarCotizacion;
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author iuga
 */
public class EditarCotizacion extends javax.swing.JInternalFrame {

    protected GestorEditarCotizacion gestor;

    protected static final int OPTN_DESCRIPCION           = 0;    
    protected static final int OPTN_HERRAMIENTAS          = 1;
    protected static final int OPTN_MATERIALES            = 2;
    protected static final int OPTN_ALQUILERES_COMPRAS    = 3;
    protected static final int OPTN_RRHH                  = 4;    
    protected static final int OPTN_GASTOSGENERALES       = 5;
    protected static final int OPTN_BENEFICIOS            = 6;
    
    public EditarCotizacion(GestorEditarCotizacion gestor)
    {
        initComponents();
        
        this.gestor = gestor;
        gestor.setPantalla(this);

        // POR DEFAULT VA DESCRIPCION DEL ITEM
        DefaultTableModel modelo = (DefaultTableModel)tblMenu.getModel();

        // Refactor !!
        initVentanaDefecto(modelo.getValueAt(OPTN_DESCRIPCION, 0).toString());
        
        ListSelectionModel selectionModel = tblMenu.getSelectionModel();
        selectionModel.setSelectionInterval(0,0);
        
        actualizar();
    }
  
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelGeneral = new javax.swing.JPanel();
        panel = new javax.swing.JScrollPane();
        btnCancelar = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        txtSubtotalSubObra = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblMenu = new javax.swing.JTable();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Sub obra");

        panelGeneral.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos Generales"));

        panel.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        panel.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        javax.swing.GroupLayout panelGeneralLayout = new javax.swing.GroupLayout(panelGeneral);
        panelGeneral.setLayout(panelGeneralLayout);
        panelGeneralLayout.setHorizontalGroup(
            panelGeneralLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 699, Short.MAX_VALUE)
        );
        panelGeneralLayout.setVerticalGroup(
            panelGeneralLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, 475, Short.MAX_VALUE)
        );

        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/iconos/var/16x16/block.png"))); // NOI18N
        btnCancelar.setText("Cerrar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("SubTotal"));
        jPanel2.setMaximumSize(new java.awt.Dimension(32767, 300));

        txtSubtotalSubObra.setBackground(new java.awt.Color(251, 250, 241));
        txtSubtotalSubObra.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        txtSubtotalSubObra.setText("$2500");
        txtSubtotalSubObra.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(252, 239, 161)));
        txtSubtotalSubObra.setOpaque(true);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txtSubtotalSubObra, javax.swing.GroupLayout.DEFAULT_SIZE, 159, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(txtSubtotalSubObra, javax.swing.GroupLayout.DEFAULT_SIZE, 24, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Menú de Recursos"));
        jPanel3.setMaximumSize(new java.awt.Dimension(50, 50));

        tblMenu.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"Descripción"},
                {"Herramientas"},
                {"Materiales"},
                {"Alquileres/Compras"},
                {"Tareas"},
                {"Gastos Varios"},
                {"Beneficios"}
            },
            new String [] {
                "Title 1"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblMenu.setIntercellSpacing(new java.awt.Dimension(10, 5));
        tblMenu.setMaximumSize(new java.awt.Dimension(300, 300));
        tblMenu.setRowHeight(25);
        tblMenu.setTableHeader(null);
        tblMenu.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                tblMenuMousePressed(evt);
            }
        });
        jScrollPane1.setViewportView(tblMenu);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 475, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnCancelar)
                    .addComponent(panelGeneral, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(panelGeneral, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancelar)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        this.gestor.refrescarPantallas();
        this.dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void tblMenuMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblMenuMousePressed

        DefaultTableModel modelo = (DefaultTableModel)tblMenu.getModel();
        clickMenuLateral(modelo);
        
    }//GEN-LAST:event_tblMenuMousePressed
    

    protected void setNombrePanel(String nombre)
    {
         panelGeneral.setBorder(javax.swing.BorderFactory.createTitledBorder(nombre));
    }
    
    public void actualizar()
    {
        DecimalFormat df = new DecimalFormat("#.00");
        
        double subTotal = gestor.calcularSubtotalSubObra();
        String montoSubTotal = df.format(subTotal).replaceAll(",",".");
        setMontoTotal(montoSubTotal.replaceAll(",","."));
        //setMontoTotal(Double.toString(gestor.calcularSubtotalSubObra()).replace(".", ","));
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane panel;
    private javax.swing.JPanel panelGeneral;
    private javax.swing.JTable tblMenu;
    private javax.swing.JLabel txtSubtotalSubObra;
    // End of variables declaration//GEN-END:variables

    protected void initVentanaDefecto(String nombre) {
        setNombrePanel(nombre);
        CotizacionDescripcion ecd = new CotizacionDescripcion(gestor.getGestorDescripcion());
        panel.setViewportView(ecd);
        ecd.setVisible(true);
    }

    protected javax.swing.JScrollPane getPanel()
    {
        return panel;
    }
    
    protected javax.swing.JTable getMenuLateral()
    {
        return tblMenu;
    }
    
    protected void clickMenuLateral(DefaultTableModel modelo)
    {
        switch(tblMenu.getSelectedRow())
        {
            case OPTN_BENEFICIOS:
                setNombrePanel(modelo.getValueAt(OPTN_BENEFICIOS,0).toString());
                CotizacionBeneficios ecb = new CotizacionBeneficios(gestor.getGestorBeneficios());
                panel.setViewportView(ecb);
                ecb.setVisible(true);
                break;
            case OPTN_MATERIALES:
                setNombrePanel(modelo.getValueAt(OPTN_MATERIALES,0).toString());
                CotizacionMateriales cm = new CotizacionMateriales(gestor.getGestorMateriales());
                panel.setViewportView(cm);
                cm.setVisible(true);
                break;
            case OPTN_ALQUILERES_COMPRAS:
                setNombrePanel(modelo.getValueAt(OPTN_ALQUILERES_COMPRAS,0).toString());
                CotizacionAlquileresCompras cc = new CotizacionAlquileresCompras(gestor.getGestorAlquileresCompras(),0);
                panel.setViewportView(cc);
                cc.setVisible(true);
                break;
            case OPTN_GASTOSGENERALES:
                setNombrePanel(modelo.getValueAt(OPTN_GASTOSGENERALES,0).toString());
                CotizacionAdicionales ca = new CotizacionAdicionales(gestor.getGestorAdicionales());
                panel.setViewportView(ca);
                ca.setVisible(true);
                break;
            case OPTN_RRHH:
                setNombrePanel(modelo.getValueAt(OPTN_RRHH,0).toString());
                CotizacionManoDeObraGeneral mo = new CotizacionManoDeObraGeneral(gestor.getGestorManoObra());
                panel.setViewportView(mo);
                mo.setVisible(true);
                break;
            case OPTN_HERRAMIENTAS:
                setNombrePanel(modelo.getValueAt(OPTN_HERRAMIENTAS, 0).toString());
                CotizacionHerramientas h = new CotizacionHerramientas(gestor.getGestorHerramientas());
                panel.setViewportView(h);
                h.setVisible(true);
                break;
            case OPTN_DESCRIPCION:
                setNombrePanel(modelo.getValueAt(OPTN_DESCRIPCION, 0).toString());
                CotizacionDescripcion ecd = new CotizacionDescripcion(gestor.getGestorDescripcion());
                panel.setViewportView(ecd);
                ecd.setVisible(true);
                break;
            default:
                setNombrePanel(modelo.getValueAt(OPTN_DESCRIPCION, 0).toString());
                CotizacionDescripcion ecd2 = new CotizacionDescripcion(gestor.getGestorDescripcion());
                panel.setViewportView(ecd2);
                ecd2.setVisible(true);
        }        
    }
    
    protected void setMontoTotal(String monto)
    {
        txtSubtotalSubObra.setText("$ "+monto);
    }

}
